//
//  HYCSeacrhBarController.m
//  FamilyTravel
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCSeacrhBarController.h"
#import "HYCHeadDocument.h"
@interface HYCSeacrhBarController ()<UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, retain)UISearchBar *searchBar;
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSArray *myArr;
@property(nonatomic, retain)NSString *tableViewStr;
@property(nonatomic, retain) NSString *utfStr;
@end

@implementation HYCSeacrhBarController
- (void)dealloc {
    [_tableViewStr release];
    [_searchBar release];
    [_myArr release];
    [_tableViewStr release];
    [_utfStr release];
    
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createData];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 66, self.view.frame.size.width, 50)];
    self.searchBar.delegate = self;
    
    self.searchBar.barStyle = UIBarStyleBlackTranslucent;
    self.searchBar.keyboardType = UIKeyboardAppearanceDefault;
    self.searchBar.placeholder = @"请输入城市名称";
    [self.view addSubview: self.searchBar];
    [_searchBar release];
    self.searchBar.showsScopeBar = YES;
    self.navigationItem.titleView = self.searchBar;
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [_tableView release];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    self.tableView.alpha = 0.5;
    
}

- (void)createData{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:@"http://open.qyer.com/qyer/hotel/hot_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=199&page=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=AB5B28ED-8670-4190-873A-F09EFE3EB3E8&track_os=ios%25207.1.2&v=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.myArr = dic[@"data"];
        
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.myArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    
    if (self.myArr.count != 0) {
      cell.textLabel.text = self.myArr[indexPath.row][@"cnname"];  
    }
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HYCPudController *pCVC = [[HYCPudController alloc] init];
    
    pCVC.str = self.myArr[indexPath.row][@"id"];
    
    [self.navigationController pushViewController:pCVC animated:YES];
   
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSString *str = searchBar.text;

    
    NSString *strUrl = [str stringByReplacingOccurrencesOfString:@" " withString:@"?"];

    NSString *url = [strUrl  stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:strUrl]];
    
   

    self.utfStr = (NSString *)CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)str, NULL, NULL,  kCFStringEncodingUTF8 );
    

    HYCSRViewController *srVC = [[HYCSRViewController alloc] init];

        srVC.gStr = strUrl;
    
     NSLog(@"****%@###",url);
        
        srVC.resStr = url;
    
        [self.navigationController pushViewController:srVC animated:YES];

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
